package welcome;

import dao.PlanDao;
import dao.impl.PlanDaoImpl;
import pojo.Plan;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;

public class DataService {
    public static Object getData(Class clazz){
        PlanDao planDao = new PlanDaoImpl();
        Plan plan = null;
        try {
            plan = planDao.getLatestPlan();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        Object pojo = null;
        try {
            Object dao = clazz.getConstructor().newInstance();
            Method[] methods = clazz.getMethods();
            for (Method method : methods){
                if (method.getName().matches("getAll")){
                    pojo = method.invoke(dao,plan.getCid());
                }
            }
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return pojo;
    }
}
